package com.example.hewei.smarttransport.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.hewei.smarttransport.Adapter.MenuAdapter;
import com.example.hewei.smarttransport.Dialog.SettingDialog;
import com.example.hewei.smarttransport.Fragment.AdminCars;
import com.example.hewei.smarttransport.Fragment.AdminETC;
import com.example.hewei.smarttransport.Fragment.AdminParking;
import com.example.hewei.smarttransport.Fragment.AdminStreetLights;
import com.example.hewei.smarttransport.Fragment.AdminTrafficLights;
import com.example.hewei.smarttransport.Fragment.AdminUsers;
import com.example.hewei.smarttransport.Fragment.Bus;
import com.example.hewei.smarttransport.Fragment.Car;
import com.example.hewei.smarttransport.Fragment.Environment;
import com.example.hewei.smarttransport.Fragment.OriginalyFragment;
import com.example.hewei.smarttransport.Fragment.Parking;
import com.example.hewei.smarttransport.Fragment.Traffic;
import com.example.hewei.smarttransport.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by 何伟 on 2017/3/14.
 */

public class SlidingMenuActivity extends BaseActivity {

    SlidingMenu mSlidingMenu;
    ListView mListMenuLV;
    Toolbar toolbar;
    Handler handler;
    boolean isAdmin;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slidingmenu);
        initView();
        setToolbar();
        setSlidingMenu();
        setListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void setToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.icon_back);
        toolbar.setTitle(R.string.app_name);
        toolbar.setSubtitle("Fragment的功能名称");
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                String msg = "";
                switch (item.getItemId()) {
                    case R.id.login:
                        msg += "弹出登录对话框，登录成功后，应该要隐藏登录菜单项，显示注销菜单项";
                        break;
                    case R.id.logout:
                        msg += "注销登录状态，隐藏注销，显示登录";
                        break;
                    case R.id.register:
                        msg += "弹出注册对话框";
                        break;
                    case R.id.setting:
                        msg += "弹出设置对话框";
                        break;

                }
                if (!msg.equals("")) {
                    Toast.makeText(SlidingMenuActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSlidingMenu.toggle();
            }
        });
    }

    private void setListener() {

        mListMenuLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                //标准动画
//                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
                //自定义
                fragmentTransaction.setCustomAnimations(R.anim.fragment_in, 0);
                Fragment fragment;
                if(isAdmin) {
                    switch (position) {
                        case 1:
                        fragment = new Car();
                            break;
                        case 2:
                            fragment = new Traffic();
                            break;
                        case 3:
                            fragment = new Parking();
                            break;
                        case 4:
                            fragment = new Bus();
                            break;
                        case 5:
                            fragment = new Environment();
                            break;
                        case 6:
                            fragment = new OriginalyFragment();
                            break;
                        default:
                            fragment = new Environment();
                            break;
                    }
                }
                else{
                    switch (position) {
                        case 1:
                            fragment = new AdminUsers();
                            break;
                        case 2:
                            fragment = new AdminCars();
                            break;
                        case 3:
                            fragment = new AdminTrafficLights();
                            break;
                        case 4:
                            fragment = new AdminParking();
                            break;
                        case 5:
                            fragment = new AdminETC();
                            break;
                        case 6:
                            fragment = new AdminStreetLights();
                            break;
                        default:
                            fragment = new Environment();
                            break;
                    }
                }
                fragmentTransaction.replace(R.id.container, fragment).commit();
                mSlidingMenu.toggle();
            }
        });
    }

    private void setSlidingMenu() {
        mSlidingMenu.setMenu(getMenu());
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new Car()).commit();
        mSlidingMenu.setShadowWidth(1);
        mSlidingMenu.setShadowDrawable(R.drawable.drawer_shadow);
        mSlidingMenu.setBehindWidth(220);
        mSlidingMenu.setFadeDegree(0.35f);
        mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        mSlidingMenu.setOnOpenListener(new SlidingMenu.OnOpenListener() {
            @Override
            public void onOpen() {
                final View view = mSlidingMenu.getRootView();
                Runnable runnable = new Runnable() {

                    @Override
                    public void run() {
                        try {
                            TextView mDatetimeTV = (TextView) view.findViewById(R.id.datetime);
                            TextView mWeekTV = (TextView) view.findViewById(R.id.week);
                            Calendar c = Calendar.getInstance();
                            Format format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                            String datetime = format.format(c.getTime());
                            mDatetimeTV.setText(datetime);
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                                mWeekTV.setText("星期天");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                                mWeekTV.setText("星期一");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
                                mWeekTV.setText("星期二");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
                                mWeekTV.setText("星期三");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
                                mWeekTV.setText("星期四");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
                                mWeekTV.setText("星期五");
                            }
                            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                                mWeekTV.setText("星期六");
                            }
                            handler.postDelayed(this, 1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                handler.post(runnable);
            }
        });
    }

    private View getMenu() {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.slidingmenu, null);
        mListMenuLV = (ListView) view.findViewById(R.id.listView);
        setHeaderView();
//        setFooterView();
        setAdapter();
        setCustomFunction(view);
        return view;
    }

    private void setCustomFunction(View view) {
        TextView mSettingTV= (TextView) view.findViewById(R.id.setting);
        mSettingTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingDialog Setting=new SettingDialog();
                Setting.show(getSupportFragmentManager(), "Setting");
            }
        });
    }

    private void setFooterView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.slidingmenu_footer, null, false);
        mListMenuLV.addFooterView(mHeaderView, null, false);
    }

    private void setHeaderView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.slidingmenu_header, null, false);
        mListMenuLV.addHeaderView(mHeaderView, null, false);
    }

    private void setAdapter() {
        String[] mListArray,mLetterArray;
        if(isAdmin) {
            mListArray=getResources().getStringArray(R.array.menu_array);
            mLetterArray = getResources().getStringArray(R.array.menu_letter);
        }
        else{
            mListArray=getResources().getStringArray(R.array.menu_array_admin);
            mLetterArray = getResources().getStringArray(R.array.menu_letter_admin);
        }
        mListMenuLV.setAdapter(new MenuAdapter(this, mListArray, mLetterArray));
    }

    private void initView() {
        mSlidingMenu = new SlidingMenu(this);
        handler = new Handler();
        isAdmin=false;
    }

}
